package vip.zhenzicheng.algorithm.leetcode.array;

import java.util.HashMap;
import java.util.Map;

/**
 * <a href="https://leetcode.cn/problems/two-sum/">1.两数之和</a>
 *
 * @author zhenzicheng
 * @date: 2022-05-20 16:34
 */
public class TwoSum_01 {
  public int[] twoSum(int[] nums, int target) {
    int N = nums.length;
    if (N == 2) {
      return new int[]{0, 1};
    }

    Map<Integer, Integer> cache = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
      if (cache.containsKey(target - nums[i])) {
        return new int[]{cache.get(target - nums[i]), i};
      }
      cache.put(nums[i], i);
    }
    return null;
  }
}
